package com.cenwangkeji.resident.ui.activity.special;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.model.bean.Xer18Bean;
import com.cenwangkeji.resident.ui.adapter.SpecialDiseaseDetailAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.view.TitleLayout;
import com.lws.basic.view.adapter.AdapterIntegerDefault0;
import com.lws.basic.view.adapter.DoubleDefault0Adapter;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class Xer18DetailActivity extends BaseActivity {

    @Override
    public int setLayoutId() {
        return R.layout.activity_xer_detail;
    }

    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.title)
    TitleLayout mTitleLayout;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mTitleLayout.setText("1到8月龄儿童健康检查记录");
        initRecycelrView();
        getData();
    }

    private void initRecycelrView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<Xer18Bean>() {
            @Override
            public void subscribe(ObservableEmitter<Xer18Bean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 3);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkHttpUtils.post(jsonObject.toString(),Constant.xer18_detail );
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, Xer18Bean.class));
            }
        }).compose(RxUtils.<Xer18Bean>io_main()).subscribe(new ObserverOnNext<Xer18Bean>() {
            @Override
            public void onNext(Xer18Bean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("月龄:  " + yl(bean.getData().getYl()));
                        list.add("父亲名字：  " + bean.getData().getFxm());
                        list.add("职业：  " + bean.getData().getFzy());
                        list.add("出生日期：  " + bean.getData().getFcsrq());
                        list.add("电话：  " + bean.getData().getFdh());
                        list.add("母亲名字：  " + bean.getData().getMxm());
                        list.add("职业：  " + bean.getData().getMzy());
                        list.add("出生日期：  " + bean.getData().getMcsrq());
                        list.add("电话：  " + bean.getData().getMdh());
                        list.add("体重/cm:  " + tz(bean.getData().getXseTz()));
                        list.add("身长/com:  " + tz(bean.getData().getXseSz()));
                        list.add("头围/cm:  " + bean.getData().getXseTw());
                        list.add("面色:  " + ms(bean.getData().getXseMs()));
                        list.add("皮肤:  " + yc(bean.getData().getXsePf()));
                        list.add("前囟:  " + qx(bean.getData().getXseQl()));
                        list.add("颈部包块:  " + (bean.getData().getXseJbbk() == 1 ? "有" : "无"));
                        list.add("眼睛:  " + yc(bean.getData().getXseYj()));
                        list.add("耳:  " + yc(bean.getData().getXseEd()));
                        if (bean.getData().getYl() == 1 || bean.getData().getYl() == 2) {
                            list.add("口腔:  " + yc(bean.getData().getXseKq()));
                        }
                        list.add("胸部:  " + yc(bean.getData().getXseXb()));
                        list.add("腹部:  " + yc(bean.getData().getXseFb()));
                        list.add("四肢:  " + yc(bean.getData().getXseSz()));
                        list.add("肛门:  " + yc(bean.getData().getXseGm()));
                        list.add("脐部:  " + qb(bean.getData().getXseQb()));
                        if (bean.getData().getYl() == 2) {
                            list.add("可疑佝偻病症状:  " + glbzz(bean.getData().getXseKyglbzz()));
                            list.add("可疑佝偻病体征:  " + glbtz(bean.getData().getXseKyglbtz()));
                        }
                        if (bean.getData().getYl() == 3 || bean.getData().getYl() == 4) {
                            list.add("听力:  " + (bean.getData().getXseTl() == 1 ? "通过" : "未通过"));
                            list.add("口腔    出牙颗数:  " + bean.getData().getXseCys());
                            list.add("可疑佝偻病症状:  " + glbzz(bean.getData().getXseKyglbzz()));
                            list.add("可疑佝偻病体征:  " + glbtz3(bean.getData().getXseKyglbtz()));
                        }
                        list.add("户外活动：  " + bean.getData().getHwhd() + "  小时/日");
                        list.add("服用维生素D:  " + bean.getData().getFywssd() + "  IU/日");
                        list.add("发育评估:  " + fypg(bean.getData().getFypg()));
                        list.add("两次随访间患病情况：\n" +
                                lcsf(bean.getData().getLcsfjhzqk(), bean.getData().getLcsfjhzqkCs(), bean.getData().getLcsfjhzqkQt()));
                        if (bean.getData().getZzjy() == 2) {// 1无 2有
                            list.add("转诊原因：" + bean.getData().getZzjyYy());
                            list.add("机构科室：" + bean.getData().getZzjyJgjks());

                        }


                        list.add("指导:  " + zd(bean.getData().getZdList(), bean.getData().getZdQt()));

                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }




    //指导
    private String zd(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s = "科学喂养  ";
                    break;
                case "2":
                    s += "生长发育  ";
                    break;
                case "3":
                    s += "疾病预防  ";
                    break;
                case "4":
                    s += "预防伤害  ";
                    break;
                case "5":
                    s += "口腔保健   ";
                    break;
                case "6":
                    s +=qt;
                    break;


            }
        }
        return s;

    }


    //两次随访患病情况
    private String lcsf(int index,int cs,String qt){
        String s ="";
        switch (index){
            case 1:
                return "无";
            case 2:
                s = "肺炎"+"   次数："+cs;
                break;
            case 3:
                s = "腹泻"+"   次数："+cs;
                break;
            case 4:
                s = "外伤"+ "   次数："+cs;
                break;
            case 5:
                s = qt;
                break;
        }
        return s;
    }

    //发育评估
    private String fypg(int index){
        String s ="";
        switch (index){
            case 1:
                s = "对很大声音没有反应";
                break;
            case 2:
                s = "逗引时不发音或不会笑";
                break;
            case 3:
                s = "不注视人脸，不追视移动人或物品";
                break;
            case 4:
                s = "俯卧时不会抬头";
                break;
        }
        return s;

    }

    //可疑佝偻病体征 3 六月龄
    private String glbtz3(int index){
        String s ="";
        switch (index){
            case 1:
                s = "无";
                break;
            case 2:
                s = "颅骨软化";
                break;


        }
        return s;
    }

    //可疑佝偻病体征
    private String glbtz(int index){
        String s ="";
        switch (index){
            case 1:
                s = "无";
                break;
            case 2:
                s = "肋串珠";
                break;
            case 3:
                s = "肋软骨沟";
                break;
            case 4:
                s = "鸡胸";
                break;
            case 5:
                s = "手足镯";
                break;
            case 6:
                s = "颅骨软化";
                break;
            case 7:
                s = "方颅";
                break;
        }
        return s;
    }

    //可疑佝偻病症状
    private String glbzz(int index){
        String s ="";
        switch (index){
            case 1:
                s = "无";
                break;
            case 2:
                s = "夜惊";
                break;
            case 3:
                s = "多汗";
                break;
            case 4:
                s = "烦躁";
                break;
        }
        return s;
    }


    //脐部
    private String qb(int index){
        String s ="";
        switch (index){
            case 1:
                s = "未脱";
                break;
            case 2:
                s = "脱落";
                break;
            case 3:
                s = "脐部有渗出";
                break;
            case 4:
                s = "其他";
                break;
        }
        return s;
    }

    //前囟
    private String qx(int index){
        String s ="";
        switch (index){
            case 1:
                s = "闭合";
                break;
            case 2:
                s = "未闭合";
                break;
        }
        return s;
    }


    //异常
    private String yc(int index){
        String s ="";
        switch (index){
            case 1:
                s = "未见异常";
                break;
            case 2:
                s = "异常";
                break;
        }
        return s;
    }

    //面色
    private String ms(int index ){
        String s ="";
        switch (index){
            case 1:
                s = "红润";
                break;
            case 2:
                s = "黄染";
                break;
            case 3:
                s = "其他";
                break;

        }
        return s;
    }


    //体重 上中下
    private String tz(int index){
        String s= "";
        switch (index){
            case 1:
                s = "上";
                break;
            case 2:
                s = "中";
                break;
            case 3:
                s = "下";
                break;


        }
        return s;

    }

    //月龄
    private String yl(int index){
        String s= "";
        switch (index){
            case 1:
                s = "满月";
                break;
            case 2:
                s = "三月龄";
                break;
            case 3:
                s = "六月龄";
                break;
            case 4:
                s = "八月龄";
                break;

        }
     return s;
    }

}
